package com.itheima.controller;


import com.github.pagehelper.PageInfo;
import com.itheima.comon.Code;
import com.itheima.comon.Result;
import com.itheima.pojo.User;
import com.itheima.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**用户模块
 * @Description:
 * @Version: V1.0
 */

@RestController
@RequestMapping("/user")
public class UserController {


    @Autowired
    private UserService userService;
    @PostMapping
    public Result save(User user){
        boolean b = userService.save(user);
        return new Result(b? Code.USER_OK:Code.USER_ERROR,b? "保存成功":"保存失败");
    }

    @PutMapping
    public Result update(User user){
        boolean b = userService.update(user);
        return new Result(b? Code.USER_OK:Code.USER_ERROR,b? "修改成功":"修改失败");
    }
    @DeleteMapping("/{uuid}")
    public Result delete(@PathVariable("uuid") Integer uuid){
        boolean b = userService.delete(uuid);
        return new Result(b? Code.USER_OK:Code.USER_ERROR,b? "删除成功":"删除失败");
    }
    @GetMapping("/{page}/{size}")
    public Result finAll(@PathVariable("page")Integer page, @PathVariable("size")Integer size){
        PageInfo<User> all = userService.findAll(page, size);
        System.out.println(all);
        return new Result(all!=null? Code.USER_OK:Code.USER_ERROR,all!=null? "查询成功":"查询失败",all);
    }

    @GetMapping("/{uuid}")
    public Result finById(@PathVariable("uuid") Integer uuid){
        User user = userService.findById(uuid);
        System.out.println(user);
        return new Result(user!=null? Code.USER_OK:Code.USER_ERROR,user!=null? "查询成功":"查询失败",user);
    }
    @PostMapping("/login")
    public Result<User> loin(String userName, String password){
        System.out.println("111111");
        User user = userService.login(userName,password);

        return new Result(user!=null? Code.USER_OK:Code.USER_ERROR,user!=null? "登录成功":"用户名或密码错误",user);
    }

    @GetMapping("/search/{page}/{size}")
    public Result getUsers(@PathVariable("page")Integer page, @PathVariable("size")Integer size,@RequestBody User user){
        PageInfo<User> all = userService.getUsers(page, size,user);
        return new Result(all!=null? Code.USER_OK:Code.USER_ERROR,all!=null? "查询成功":"查询失败",all);
    }
}
